package com.sinothk.fc.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.fc.customer.entity.CustomerEntity;
import com.sinothk.fc.customer.entity.CustomerReq;
import com.sinothk.fc.customer.mapper.CustomerMapper;
import com.sinothk.fc.customer.service.ICustomerService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.PinyinTools;
import org.springframework.stereotype.Service;

/**
 * @Description: 门店客户表
 * @Author: Liangyt
 * @Date: 2022-09-11
 * @Version: V1.0
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, CustomerEntity> implements ICustomerService {

    @Override
    public IPage<CustomerEntity> pageListForStore(Page<CustomerEntity> page, CustomerReq customerReq) {
        LambdaQueryWrapper<CustomerEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerEntity::getStoreId, customerReq.getStoreId());
        if (StringUtils.isNotBlank(customerReq.getKeywords())) {
            wrapper.likeLeft(CustomerEntity::getNamePy, customerReq.getKeywords());
        }
        wrapper.orderByDesc(CustomerEntity::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    public void add(CustomerEntity customerEntity) throws Exception {
        if (StringUtils.isBlank(customerEntity.getName())) throw new Exception("顾客姓名不能为空");
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        customerEntity.setStoreId(loginUser.getStoreId());
        customerEntity.setServerId(loginUser.getId());

        String pinyinSm = PinyinTools.getPinyinFirstLetterUpperCase(customerEntity.getName());
        customerEntity.setNamePy(pinyinSm);
        this.save(customerEntity);
    }
}
